package cn.zn.dict.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zn.base.common.constant.Constants;
import cn.zn.redis.service.RedisService;
import cn.zn.dict.entity.SysDictValue;
import cn.zn.dict.mapper.SysDictValueMapper;
import cn.zn.dict.service.SysDictTypeService;
import cn.zn.dict.service.SysDictValueService;
import cn.zn.dict.vo.SysDictValueEditVo;
import cn.zn.dict.vo.SysDictValuePageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zn-admin
 * @since 2021-01-28
 */
@Service
public class SysDictValueServiceImpl extends ServiceImpl<SysDictValueMapper, SysDictValue> implements SysDictValueService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private SysDictTypeService dictTypeService;

    @Override
    public Object selectDictValueList(SysDictValuePageVo vo) {
        if (!vo.getPage()) {
            return baseMapper.selectDictValueList(vo);
        }

        Page<SysDictValue> page = new Page<>(vo.getCurrent(), vo.getSize());
        return baseMapper.selectDictValueList(page, vo);
    }

    @Override
    public List<SysDictValue> selectDictValueByType(String dictType) {
        List<SysDictValue> dictValues = getDictCache(dictType);
        if (ObjectUtil.isNotEmpty(dictValues)) {
            return dictValues;
        }

        QueryWrapper<SysDictValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "0");
        queryWrapper.eq("dict_type", dictType);
        queryWrapper.orderByAsc("dict_sort");
        dictValues = baseMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(dictValues)) {
            redisService.setCacheObject(getCacheKey(dictType), dictValues);
            return dictValues;
        }
        return new ArrayList<>();
    }

    @Override
    @Transactional
    public void insertDictValue(SysDictValueEditVo vo) {
        SysDictValue dictValue = new SysDictValue();
        BeanUtil.copyProperties(vo, dictValue);
        boolean success = dictValue.insert();
        if (success) {
            dictTypeService.clearDictCache();
        }
    }

    @Override
    @Transactional
    public void updateDictValue(String id, SysDictValueEditVo vo) {
        SysDictValue dictValue = new SysDictValue();
        BeanUtil.copyProperties(vo, dictValue);
        dictValue.setId(id);
        boolean success = dictValue.updateById();
        if (success) {
            dictTypeService.clearDictCache();
        }
    }

    @Override
    @Transactional
    public void deleteDictValueByIds(String[] ids) {
        int row = baseMapper.deleteBatchIds(Arrays.asList(ids));
        if (row > 0) {
            dictTypeService.clearDictCache();
        }
    }

    /**
     * 获取字典缓存
     *
     * @param key 参数键
     * @return dictDatas 字典数据列表
     */
    @SuppressWarnings("unchecked")
    private List<SysDictValue> getDictCache(String key) {
        Object cacheObj = redisService.getCacheObject(getCacheKey(key));
        if (ObjectUtil.isNotNull(cacheObj)) {
            return (List<SysDictValue>) cacheObj;
        }
        return null;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey) {
        return Constants.SYS_DICT_KEY + configKey;
    }

}
